package com.watson.webService.config;

import com.watson.webService.service.DemoService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

/**
 * 实现WebService客户端,生成客户端的代码，
 * CXF有两种调用方式，代理调用和动态程序调用。
 *
 * @author: llun
 * @date: 2022/2/16 15:44
 * @version: 1.0
 */
public class demo {

    /**
     * 动态调用：
     *
     * @param args
     */
//    public static void main(String[] args) {
//        //创建动态客户端
//        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
//        Client client = factory.createClient("http://localhost:8868/demo/api?wsdl");
//        // 需要密码的情况需要加上用户名和密码
//        client.getOutInterceptors().add(new ClientLoginInterceptor("admin","123456"));
//        HTTPConduit conduit = (HTTPConduit) client.getConduit();
//        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
//        httpClientPolicy.setConnectionTimeout(2000);  //连接超时
//        httpClientPolicy.setAllowChunking(false);    //取消块编码
//        httpClientPolicy.setReceiveTimeout(120000);     //响应超时
//        conduit.setClient(httpClientPolicy);
//        //client.getOutInterceptors().addAll(interceptors);//设置拦截器
//        try {
//            Object[] objects = new Object[0];
//            // invoke("方法名",参数1,参数2,参数3....);
//            objects = client.invoke("sayHello", "sujin");
//            System.out.println("返回数据:" + objects[0]);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    private static final String ADDRESS = "http://localhost:8868/demo/api?wsdl"; // WebService服务地址


    /**
     * 代理调用:
     */
    public static void main(String[] args) {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setAddress(ADDRESS);
        jaxWsProxyFactoryBean.setServiceClass(DemoService.class);
        jaxWsProxyFactoryBean.getOutInterceptors().add(
                // 设置用户名，密码
                new ClientLoginInterceptor("admin", "123456")
        );
        DemoService demoService = (DemoService) jaxWsProxyFactoryBean.create();
        String echo = demoService.sayHello("xiaoming");
        System.out.println("echo = " + echo);
    }


}
